package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.CheckItemDeleteFailException;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private PermissionDao permissionDao;


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer start = queryPageBean.getCurrentPage();
        Integer size = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(start,size);
        List<Role> roleList;
        if(queryString == null ){
            roleList = roleDao.findPageNotQueryString();
        }else{
            roleList = roleDao.findPage(queryString);
        }
        PageInfo pageInfo = new PageInfo(roleList);
        return new PageResult(pageInfo.getTotal(),roleList);
    }

    @Override
    public Result getAllPermission(){
        try {
            List<Permission> permissionList = permissionDao.getAllPermission();
            return new Result(true,"查询权限信息成功",permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"查询权限信息失败");
        }

    }

    @Override
    public Result getAllMenu() {
        try {
            List<Menu> menuList = menuDao.getAllMenu();
            return new Result(true,"查询菜单信息成功",menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"查询菜单信息失败");
        }
    }

    // 添加角色 checkitemId1s 菜单id  checkitemIds 权限id
    @Override
    public Result add(Integer[] checkitemIds, Integer[] checkitemId1s, Role role) {
        try {
            roleDao.insertRole(role);
            Integer roleId = role.getId();
            // 添加权限
            for (Integer checkitemId : checkitemIds) {
                permissionDao.insertRoleIdAndPermissionId(roleId,checkitemId);
            }
            for (Integer checkitemId1 : checkitemId1s) {
                menuDao.insertRoleIdAndMenuId(roleId,checkitemId1);
            }
            return new Result(true,"新增角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增角色失败");
        }
    }

    @Override
    public Result findById(Integer userId) {
        try {
            Role role = roleDao.findById(userId);
            return new Result(true,"查询角色信息成功",role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色信息失败");
        }
    }

    @Override
    public Result permissionIds(Integer roleId) {
        try {
            List<Integer> permissionIds = permissionDao.getByRoleId(roleId);
            return new Result(true,"查询权限信息成功",permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询权限信息失败");
        }

    }

    @Override
    public Result findRoleByMenuId(Integer roleId) {
        try {
            List<Integer> menuIds = menuDao.getByRoleId(roleId);
            return new Result(true,"查询权限信息成功",menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询权限信息失败");
        }

    }

    @Override
    public Result edit(Integer[] checkitemIds, Integer[] checkitemId1s, Role role) {
        try {
            roleDao.updateRole(role);
            permissionDao.delectRleIdAndPermission(role.getId());
            menuDao.deleteRoleIdAndMenu(role.getId());
            for (Integer checkitemId : checkitemIds) {
               permissionDao.insertRoleIdAndPermissionId(role.getId(),checkitemId);
            }
            for (Integer checkitemId1 : checkitemId1s) {
                menuDao.insertRoleIdAndMenuId(role.getId(),checkitemId1);
            }
            return new Result(true,"修改角色信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"修改角色信息失败");
        }
    }

    @Override
    public Result delete(Integer roleId){
        try{
            Integer userCount = userDao.getRoleIdCount(roleId);
            if(userCount ==  0){
                menuDao.deleteRoleIdAndMenu(roleId);
                permissionDao.delectRleIdAndPermission(roleId);
                roleDao.delete(roleId);
            }else{
                throw new CheckItemDeleteFailException();
            }
            return new Result(true,"删除角色信息成功");
        }catch (Exception e){
            e.printStackTrace();
            if(e instanceof CheckItemDeleteFailException){
                return new Result(false, "删除角色信息失败，请收回所有用户的该角色");
            }
            return new Result(false,"删除失败，请稍后再试");
        }
    }
}
